<template>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="用户名称" prop="name" />
    <el-table-column label="用户账号" prop="account"></el-table-column>
    <el-table-column label="用户类型" prop="type">
      <template #default="scope">{{ scope.row.type === '2' ? '管理员' : '普通用户' }}</template>
    </el-table-column>
    <el-table-column align="right">
      <template #header>
        <el-form style="display: flex;">
          <el-button type="primary" @click="handleCreate" size="small">新建</el-button>
          <el-input v-model="search" size="small" placeholder="搜索用户名称" width="100" />
        </el-form>
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          修改
        </el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row)">
          删除
        </el-button>
        <el-button size="small" @click="handleResetPassword(scope.row)"> 重置密码 </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="centerDialogVisible" :title="titleDialog" width="500" @open="handleOpenDialog">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="用户账号" prop="account">
        <el-input v-model="formData.account" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleSubmit">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { useHandleData } from '@/hooks/useHandleData'
import { userList, editUser, changePassword } from '@/api/modules/index'
import { computed, onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'

interface User {
  id: number
  account: string
  name: string
  type: number
}

const search = ref('')
const centerDialogVisible = ref(false)
const openDialog = ref(false)
const formData = ref({ name: '', account: '', id: 0 })
const tableData = ref<User[]>([])
const formRef = ref()


const formRules = {
  name: [{ required: true, message: '请输入用户名称', trigger: 'blur' },],
  account: [{ required: true, message: '请输入用户账号', trigger: 'blur' },],
}
const titleDialog = computed(() => (openDialog.value ? '新建用户' : '修改用户'))
const filterTableData = computed(() =>
  (tableData.value || []).filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
)

const handleOpenDialog = () => {
  formRef.value.clearValidate();
  fetchData()
}
const fetchData = async () => {
  const res = await userList()
  tableData.value = res.data.data.list
}
onMounted(() => {
  fetchData()

})
const handleCreate = () => {
  openDialog.value = true
  formData.value = { name: '', account: '', id: 0 }
  centerDialogVisible.value = true
}
const handleEdit = (row: User) => {
  openDialog.value = false
  formData.value = { name: row.name, account: row.account, id: row.id }
  centerDialogVisible.value = true
}
const handleDelete = (row: User) => {
  fetchData()
  console.log(row);

}
const handleSubmit = async () => {
  await formRef.value.validate();
  const data = { name: formData.value.name, account: formData.value.account }
  if (!openDialog.value) {
    const params = { id: formData.value.id, data }
    useHandleData(editUser, params, "修改用户", "success")
  } else {
    if (formData.value.name && formData.value.account) return;
  }
  centerDialogVisible.value = false
}
const handleResetPassword = (row: User) => {
  changePassword({
    id: row.id, data: {
      oldPassword: '',
      newPassword: ''
    }
  })
  ElMessage({ message: '重置密码为【123456】', type: 'success' })
}
</script>
